package org.way.iam.component;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.way.iam.constants.IamConstants;
import org.way.iam.entity.SysUserEntity;
import org.way.iam.service.ISysDeptService;

import javax.annotation.Resource;

@Component
@Slf4j
public class SaTokenComponent {

    @Resource
    private ISysDeptService sysDeptService;

    public void setSession(SysUserEntity user) {
        JSONObject object = new JSONObject();
        object.putOnce("username",user.getUsername());
        object.putOnce("userId",user.getId());
        object.putOnce("deptList",sysDeptService.getDeptList(user.getId()));
        object.putOnce("roleList", StpUtil.getRoleList());
        object.putOnce("menuPermissionList",StpUtil.getPermissionList());
        SaSession session = StpUtil.getSession(true);
        session.set(IamConstants.USER_INFO,object);
        log.debug("登录成功，设置session: {}",object);
    }

    public static void main(String[] args) {
        for(int i=0;i<20;i++) {
            System.out.println(IdUtil.getSnowflake().nextIdStr());
        }
    }
}
